package ibm.cognos.imt

/**
 * Created with IntelliJ IDEA.
 * User: eric
 * Date: 1/6/14
 * Time: 6:14 PM
 * To change this template use File | Settings | File Templates.
 */
class Table extends ReportElement{
    def rowCount
    def columnCount
    def cells

    def java.util.List elementList = new ArrayList()

//    def add(closure) {
//        closure.delegate = this
//        closure.resolveStrategy = Closure.DELEGATE_FIRST
//        closure.call()
//    }

    Table(closure){
        closure.delegate = this
        closure.call()
    }

    Table(r, c) {
        rowCount = r
        columnCount = c
        cells = new Object[rowCount]
        (0..<rowCount).each{row->
            cells[row] = new Object[columnCount]
            (0..<columnCount).each{ col->
                cells[row][col] = []
            }
        }
    }

    def cell(rowIndex, colIndex) {
        cells[rowIndex][colIndex]
    }

    def row(index){

    }

    public Table leftShift(object) {
        if(object instanceof ArrayList){
             object.each{
                addObject(it)
             }
        }else{
             addObject(object)
        }
        this
    }

    def addObject(object) {
        elementList.add(object)
    }

    def size(){
        return elementList.size()
    }

    def column(colIndex) {

    }

    def generateSpec(builder) {
        def index = 0
        builder.table {
            style {
                defaultStyles {
                    defaultStyle(refStyle: "tb")
                }
            }
            tableRows {
                for(int i=0;i<rowCount;i++) {
                    tableRow {
                        tableCells {
                            for(int j=0;j<columnCount;j++) {

                                tableCell {
                                    contents {
                                        def element = elementList.get(index++)
                                        element?.generateSpec(builder)
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}

